package netty.nio;

import netty.common.Constant;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author cyt
 * @create 2020-11-04 14:24
 */
public class TestC {
    public static void main(String[] args) throws IOException {
        String filepath = Constant.FILEPATH;
        File file = new File(filepath + "channel.txt");
        File fileCopy = new File(filepath + "channel_Copy.txt");
        FileChannel input = new FileInputStream(file).getChannel();
        FileChannel out = new FileOutputStream(fileCopy).getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(1014);
        while (true) {
            buffer.clear();
            /**
             * 把文件通道之中的数据 写入到内存块之中。
             */
            int read = input.read(buffer);
            if (read == -1) {
                break;
            }
            buffer.flip();
            /*
            把内存块中的数据写入到通道里面。
             */
            out.write(buffer);
        }

        System.out.println("复制结束- 》 》");
    }
}
